package com.choosefine.it.worker.controller;

import java.util.HashMap;
import java.util.Map;

import com.choosefine.it.worker.dao.FindWorkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.worker.model.FindWork;
import com.choosefine.it.worker.model.dto.PublishMgtWorkerDTO;
import com.choosefine.it.worker.model.dto.UpdateWorkerPublishmgtDTO;
import com.choosefine.it.worker.model.vo.PublishMgtWorkerVO;
import com.choosefine.it.worker.service.FindWorkService;
import com.choosefine.it.worker.service.WorkerCenterService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(value = "控制台-工人发布状态管理", description = "鞠成阳")
@RestController
@RequestMapping("/publishmgt")
public class WorkerPublishmgtController extends BaseController {

	@Autowired
	private FindWorkService findWorkService;
	
	@Autowired
	WorkerCenterService workerCenterService;
	@Autowired
	CustomRestTemplate restTemplate;
	@Autowired
	FindWorkMapper findWorkMapper;/*yuxin*/

	@Autowired
	ObjectMapper jsonObjectMapper;

	@ApiOperation(value = "根据ID修改工人发布状态")
	@ApiResponses({ @ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "系统错误，无数据") })
	@RequestMapping(value = "/updateWorkerPublishmgt", method = RequestMethod.POST)
	public String updateWorkerPublishmgt(@RequestBody UpdateWorkerPublishmgtDTO updateWorkerPublishmgtDTO) {
		String result = null;
		FindWork record = null;
		int num = 0;// 修改个数
		//发布不通过 ，必须填写反馈信息
		if(updateWorkerPublishmgtDTO.getId() == null
				||(updateWorkerPublishmgtDTO.getVerifyStatus()==2 
					&& (null == updateWorkerPublishmgtDTO.getReason() 
						|| null == updateWorkerPublishmgtDTO.getReason().trim()))){
			return responseFail();
		}
		record=findWorkMapper.selectByPrimaryKey(updateWorkerPublishmgtDTO.getId());
		if(null != record){
			record.setVerifyStatus(updateWorkerPublishmgtDTO.getVerifyStatus());
			if(null != updateWorkerPublishmgtDTO.getReason()){
				record.setReason(updateWorkerPublishmgtDTO.getReason());
			}
			num = findWorkService.updatePublishWorkStatusByRecord(record);
		}
		if (num > 0) {
			result = responseSuccess(num);
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}

		return result;
	}

	@ApiOperation(value = "按条件查找工人发布管理信息",response=PublishMgtWorkerVO.class)
	@RequestMapping(value = "/getWorkerPublishmgtInfoByType", method = RequestMethod.POST)
	public String getWorkerPublishmgtInfoByType(@RequestBody PublishMgtWorkerDTO publishMgtWorkerDTO) throws BusinessException {
		String result = null;
		Map<String, Object> map = new HashMap<>();
		if (publishMgtWorkerDTO.getUserCode() != null) {
			map.put("userCode", publishMgtWorkerDTO.getUserCode());
		}
		if(publishMgtWorkerDTO.getWorkTypeId()!=null){
			map.put("workTypeId", publishMgtWorkerDTO.getWorkTypeId());
		}
		if (publishMgtWorkerDTO.getWorkTypeName() != null&&!"".equals(publishMgtWorkerDTO.getWorkTypeName())) {
			map.put("workTypeName", publishMgtWorkerDTO.getWorkTypeName());
		}
		if (publishMgtWorkerDTO.getStartTime() != null && publishMgtWorkerDTO.getEndTime() != null) {
			map.put("startTime", publishMgtWorkerDTO.getStartTime());
			map.put("endTime", publishMgtWorkerDTO.getEndTime());
		}
		if (publishMgtWorkerDTO.getVerifyStatus() != null && !"".equals(publishMgtWorkerDTO.getVerifyStatus())) {
			map.put("verifyStatus", publishMgtWorkerDTO.getVerifyStatus());
		}
//		if(publishMgtWorkerDTO.getPublisher()!=null&&!"".equals(publishMgtWorkerDTO.getPublisher())){
//			map.put("publisher", publishMgtWorkerDTO.getPublisher());
//		}
		/*
		 * 第二版原 publisher（发布人）字段，变更为 代发布中操作员编号
		 */
		if(publishMgtWorkerDTO.getUserName()!=null&&!"".equals(publishMgtWorkerDTO.getUserName())){
			map.put("userName", publishMgtWorkerDTO.getUserName());
		}
		PageDTO pageDTO = new PageDTO();
		pageDTO.setPageNum(publishMgtWorkerDTO.getPageNum());
		pageDTO.setPageSize(publishMgtWorkerDTO.getPageSize());
		Page<FindWork> resultPage = workerCenterService.queryPublishWorkMgt(map, pageDTO);
		if(resultPage!=null && resultPage.size() > 0){
			result = responseSuccess(resultPage.toPageInfo());
		}else{
			result = responseFail();
		}
		return result;
	}
	@ApiOperation(value="根据ID软删除工人找工作信息(可批量)")
	@ApiImplicitParam(name = "ids", value = "工人IDs", required = true, dataType = "String", paramType = "path")
	@RequestMapping(value="/deleteWorkerPublishmgt/{ids}",method=RequestMethod.PUT)
	public String deleteWorkerPublishmgt(@PathVariable String ids){
		String result = null;
		Integer num = 0;// 修改个数
		String[] idsStr = ids.split(",");
		for (int i = 0; i < idsStr.length; i++) {
			if (idsStr[i] != null) {
				Integer n = findWorkService.deleteWorkerPublishmgtByPrimaryKey(Integer.parseInt(idsStr[i]));
				if (n > 0) {
					num += n;
				}
			}
		}
		if (num > 0) {
			result = responseSuccess(num);
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}

		return result;
	}
}
